﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Xml.Linq;

using Perseus;
using Perseus.Xml;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Plugins.General.Network {
    public class ClientConfiguratorItem : IConfiguratorItem, IData {
        private static ClientConfigurator _Configurator;

        private string _Name;
        private string _Identifier;
        private bool _Broadcast;

        public string Identifier {
            get { return this._Identifier; }
            set {
                if (this._Identifier.IsNot(value)) {
                    this._Identifier = value;
                    this.OnItemNameChanged(EventArgs.Empty);
                }
            }
        }
        public bool Broadcast {
            get { return this._Broadcast; }
            set {
                if (this._Broadcast != value) {
                    this._Broadcast = value;
                    this.OnItemNameChanged(EventArgs.Empty);
                }
            }
        }

        #region IConfiguratorItem Members
        public UserControl ConfigPanel {
            get {
                if (ClientConfiguratorItem._Configurator == null) {
                    ClientConfiguratorItem._Configurator = new ClientConfigurator();
                }
                ClientConfiguratorItem._Configurator.InitializeConfig(this);
                return ClientConfiguratorItem._Configurator;
            }
        }
        public bool IsEmpty { get { return this.Name.IsEmpty(); } }
        public bool IsMatch(string filter) {
            string[] filters = filter.ToLower().Split(" ", SplitOptions.RemoveEmptyEntries);

            foreach (string f in filters) {
                if (this.Name.IndexOf(f) >= 0) {
                    continue;
                }

                if (this.Identifier.IndexOf(f) >= 0) {
                    continue;
                }

                return false;
            }

            return true;
        }
        public event EventHandler ItemNameChanged;
        protected virtual void OnItemNameChanged(EventArgs e) {
            if (this.ItemNameChanged != null) {
                this.ItemNameChanged(this, e);
            }
        }

        public string Name {
            get { return this._Name; }
            set {
                if (this._Name.IsNot(value)) {
                    this._Name = value;
                    this.OnItemNameChanged(EventArgs.Empty);
                }
            }
        }
        #endregion

        #region IData Members
        public void Load(System.Xml.Linq.XElement element) {
            this.Broadcast = element.Attribute("Broadcast").BoolOrFalse();
            this.Name = element.Element("Name").ValueOrEmpty();
            this.Identifier = element.Element("Identifier").ValueOrEmpty();
        }

        public System.Xml.Linq.XElement Save() {
            if (this.IsEmpty) {
                return null;
            }

            XElement xe = new XElement("Client");

            xe.Add(new XAttribute("Broadcast", this.Broadcast));
            xe.Add(new XElement("Name", this.Name));
            xe.Add(new XElement("Identifier", this.Identifier));
            
            return xe;
        }
        #endregion
    }
}
